*****************************************************************************
	*** Figure 6 ***
******************************************************************************
	
	//Use main data
	use "${maindta}", clear

*OLS & RIF: Regressions over distribution
			
	*program for retaining values
	capture program drop retainresults
	program define retainresults
	local eb=`r(estimate)'
	local ub=`r(ub)'
	local lb=`r(lb)'
	replace outcome="$var" in $counter
	replace eb=`eb' in $counter
	replace ub=`ub' in $counter
	replace lb=`lb' in $counter
	replace quantile="$q" in $counter
 	end
	
*Run for the full distribution (not centered)
		
		*create variables for silly program
		cap drop outcome eb ub lb quantile 
		gen outcome=""
		gen eb=.
		gen ub=.
		gen lb=.
		gen quantile=""
		
		
		*get the SD of these
		preserve
		collapse (mean) CPIscore2017 polconiii vae_rs pve_rs gee_rs rqe_rs rle_rs cce_rs, by(country)
			foreach var in CPIscore2017 polconiii vae_rs pve_rs gee_rs rqe_rs rle_rs cce_rs  {
					su `var'
					global `var'_sd=`r(sd)'
			}
		global pwrshift_last5yrs_sd=1 //dummy measure
		restore 
		
		*For various governance indices
		global counter=1
		foreach var in CPIscore2017 polconiii vae_rs pve_rs gee_rs rqe_rs rle_rs cce_rs pwrshift_last5yrs {
			est clear 
		    foreach q in mean 10 25 50 75 90  {
				global var "`var'"
				global q   "`q'"
				# delimit ;
				if "`q'"=="mean" {; 
					eststo: reg  firm_score_est `var'      [pw=wt], cluster(strata_all) ; lincom _b[`var']*${`var'_sd};  retainresults;  global counter=$counter +1 ; 
				};
				if "`q'"!="mean" {; 
					eststo: rifhdreg firm_score_est `var'  [pw=wt], rif(q(`q')) cluster(strata_all); lincom _b[`var']*${`var'_sd}; retainresults;  global counter=$counter +1 ;
				};
				#delimit cr
			}
			*esttab using "${outtables}\\rif_ols_`var'.tex", replace b(3) se(3) mtitles("$\beta_{OLS}$" "$\beta_{RIF, p=10}$" "$\beta_{RIF, p=25}$" "$\beta_{RIF, p=50}$" "$\beta_{RIF, p=75}$" "$\beta_{RIF, p=90}$") label star(* 0.10 ** 0.05 *** 0.01) booktabs  title("OLS and RIF-OLS Co-efficients \label{`var'_rif}") /*
			**/ addnotes("Results from OLS and RIF-OLS (unconditional quantile regression) estimations." "All estimates use survey weights and use S.E.s adjusted for clustering at the level of sampling stratum." )
		}
		
		*Graph output
		cap drop qtile
		clonevar qtile=quantile
		destring qtile, force replace //ok to lose string value
		
		cap drop eb_ols 
		cap drop _eb
		gen _eb=eb if quantile=="mean"
		replace _eb=-9999 if outcome!="" & _eb==. //this is awful code but hell it works
		bys outcome: egen eb_ols=max(_eb)
		drop _eb
		
		cap drop lb_ols 
		cap drop _lb
		gen _lb=lb if quantile=="mean"
		replace _lb=-9999 if outcome!="" & _lb==. //this is awful code but hell it works
		bys outcome: egen lb_ols=max(_lb)
		drop _lb
		
		
		cap drop ub_ols 
		cap drop _ub
		gen _ub=ub if quantile=="mean"
		replace _ub=-9999 if outcome!="" & _ub==. //this is awful code but hell it works
		bys outcome: egen ub_ols=max(_ub)
		drop _ub
		
		*global of options
		global opts1 "& inlist(qtile, 10,90), c(l l l) xlab(, nogrid notick) ylab(,nogrid notick) msymbol(none none none) lpattern(longdash shortdash shortdash) lcolor(gs5%50 gs10%50 gs10%50)"
		global opts2 "xlab(10 "10" 25 "25" 50 "Median" 75 "75" 90 "90")  yline(0, lcolor(red) lstyle(solid)) sort fcolor(blue%40) lcolor(blue%50)"
		global opts3 "msymbol(smcircle) legend(off) mcolor(blue) msize(medium) msize(large) xtitle("")   "

		*loop over vars
		foreach var in CPIscore2017 polconiii vae_rs pve_rs gee_rs rqe_rs rle_rs cce_rs pwrshift_last5yrs {
			local label: var label `var'
			graph twoway scatter eb_ols ub_ols lb_ols qtile if outcome=="`var'" 	$opts1 || rarea ub lb qtile if outcome=="`var'", $opts2 || scatter eb qtile if outcome=="`var'" , $opts3  name(raw_`var',replace) title("{&beta}{subscript: `label'}", size(huge)) xsize(2) ysize(2) nodraw
		}
		graph combine raw_CPIscore2017 raw_polconiii raw_vae_rs raw_pve_rs raw_gee_rs raw_rqe_rs raw_rle_rs raw_cce_rs raw_pwrshift_last5yrs, row(3) col(3) xsize(7) ysize(7)  xcommon 
		
		graph export "${outfigures}\Figure6.pdf", replace
	